﻿
(function (window, jQuery, undefined) {
    "use strict";
    var $ = jQuery;

    if (window.lanHomepage === undefined) {
        window.lanHomepage = {};
    }
    if (window.lanHomepage.mainpage === undefined) {
        window.lanHomepage.mainpage = {};
    }

    var publics = {

        toggleVisibility: function () {

            if ($(".new-news-wrapper").is(":visible")) {
                $(".new-news-wrapper").hide();
            }
            else {
                $(".new-news-wrapper").show();
            }
        },
        editNewsPost: function (id) {
            var headline = $("#" + id).children(".news-post-headline").text();
            var message = $("#" + id).children(".news-post-message").text();

            console.log(headline + " : " + message);

            $("#" + id).children(".news-post-headline").hide();
            $("#" + id).children(".news-post-message").hide();
            $("#" + id).children(".news-post-date").hide();


            $("#" + id).find(".edit-news-headline").val(headline);
            $("#" + id).find(".edit-news-message").val(message);

            $("#" + id).find(".edit-news-wrapper").show();
        },
        confirmEdit: function (id) {

            var headline = $("#" + id).find(".edit-news-headline").val();
            var message = $("#" + id).find(".edit-news-message").val();

            $.msg({
                bgPath: "/Images/",
                content: '<p>Edit this news?</p>' +
                         '<p class="btn-wrap">' +
                            '<span id="yes">Yes</span>' +
                            '<br/>' +
                            '<span id="no">no</span>' +
                         '</p>',
                autoUnblock: false,
                clickUnblock: false,
                afterBlock: function () {
                    $("#yes").bind("click", function () {
                        $.ajax({
                            url: "/Main/EditNewsPost",
                            type: "POST",
                            data: JSON.stringify({ _id: id, _headline: headline, _news_text: message }),
                            contentType: "application/json; charset=utf-8",
                            success: function (data, result) {
                                if (data == "True") {
                                    $.msg("replace", 'News edited.');
                                    $.msg("unblock", "2000");

                                    $("#" + id).children(".news-post-headline").text(headline);
                                    $("#" + id).children(".news-post-message").text(message);
                                    publics.cancelEdit(id);
                                }
                                else {
                                    console.log("data: " + data + ", result: " + result);

                                    $.msg("replace", 'Error detected - id not found / check console');
                                    $.msg("unblock", "2000");
                                }
                            },
                            error: function (data, result) {
                                console.log("data: " + data + ", result: " + result);

                                $.msg("replace", 'Error detected - connection shizzle perhaps / check console');
                                $.msg("unblock", "2000");
                            }
                        });
                    });
                    $("#no").bind("click", function () {
                        $.msg("unblock", "0");
                    });
                }
            });
        },
        cancelEdit: function (id) {

            $("#" + id).children(".news-post-headline").show();
            $("#" + id).children(".news-post-message").show();
            $("#" + id).children(".news-post-date").show();

            $("#" + id).find(".edit-news-wrapper").hide();

        },
        deleteNewsPost: function (id) {

            console.log(id);

            $.msg({
                bgPath: "/Images/",
                content: '<p>Delete this news?</p>' +
                         '<p class="btn-wrap">' +
                            '<span id="yes">Yes</span>' +
                            '<br/>' +
                            '<span id="no">no</span>' +
                         '</p>',
                autoUnblock: false,
                clickUnblock: false,
                afterBlock: function () {
                    $("#yes").bind("click", function () {
                        $.ajax({
                            url: "/Main/DeleteNewsPost",
                            type: "POST",
                            data: JSON.stringify({ _id: id }),
                            contentType: "application/json; charset=utf-8",
                            success: function (data, result) {
                                if (data == "True") {
                                    $("#" + id).remove();
                                    $.msg("replace", 'News deleted.');
                                    $.msg("unblock", "2000");
                                }
                                else {
                                    console.log("data: " + data + ", result: " + result);

                                    $.msg("replace", 'Error detected - id not found / check console');
                                    $.msg("unblock", "2000");
                                }
                            },
                            error: function (data, result) {
                                console.log("data: " + data + ", result: " + result);

                                $.msg("replace", 'Error detected - connection shizzle perhaps / check console');
                                $.msg("unblock", "2000");
                            }
                        });

                    });
                    $("#no").bind("click", function () {
                        $.msg("unblock", "0");
                    });
                }
            });
        }
    }
    $.extend(window.lanHomepage.mainpage, publics);
})(window, jQuery);
